package cn.cloudtogo.server.handler;

import cn.cloudtogo.common.GendbException;
import cn.cloudtogo.server.ErrorWebResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
@Order(0)
@Slf4j
public class GendbExceptionHandler {


    @ExceptionHandler(value = GendbException.class)
    public ResponseEntity<ErrorWebResult> handle(GendbException ex, HttpServletRequest request) {
        log.error("{}", ex.getMessage(), ex);
        ErrorWebResult error = ErrorWebResult.error(HttpStatus.valueOf(ex.getCode()), request.getRequestURI(), ex.getMessage());
        return ResponseEntity.status(HttpStatus.valueOf(ex.getCode())).body(error);
    }

}
